package com.yyk.yiliao.ui.message;


import android.widget.FrameLayout;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;

import java.util.List;

import butterknife.BindView;

public class ConversationListActivity extends BaseActivity {
    private static final String TAG = "ConversationListActivit";
    public static boolean flagMessage = false;
    public static   ConversationListActivity intance = null;
    @BindView(R.id.container) //会话列表
            FrameLayout container;
    private ConversationListFragment conversationListFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_conversation_list;
    }

    @Override
    public void initView() {
        initToolbar();
        initHuihua();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("消息中心");
    }

    @Override
    public void initData() {
        intance=this;
    }

    private void initHuihua() {
        //new出EaseChatFragment或其子类的实例
        conversationListFragment = new ConversationListFragment();
        //通过getSupportFragmentManager启动fragment即可
        getSupportFragmentManager().beginTransaction().add(R.id.container, conversationListFragment).commit();
    }


    public void upateListNumber() {
        // refresh conversation list
        if (conversationListFragment != null) {
            conversationListFragment.refresh();
        }
    }


    private void refreshUIWithMessage() {
        runOnUiThread(new Runnable() {
            public void run() {
                // refresh unread count
                updateUnreadLabel();
                upateListNumber();

            }
        });
    }

    /**
     * 1.更新环信发送的消息数量
     */

    public void updateUnreadLabel() {
        int count = getUnreadMsgCountTotal();
        if (count > 0) {
            setTitle("消息中心" + "(" + count + ")");
        } else {
            setTitle("消息中心");
        }
    }

    /**
     * 获取消息数量
     *
     * @return
     */
    public int getUnreadMsgCountTotal() {
        return EMClient.getInstance().chatManager().getUnreadMsgsCount();
    }

    @Override
    protected void onResume() {
        super.onResume();
        flagMessage = true;
        updateUnreadLabel();
        upateListNumber();
        //监听消息
//        EMClient.getInstance().chatManager().addMessageListener(msgListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
//        EMClient.getInstance().chatManager().removeMessageListener(msgListener);
    }

    /**
     * 2.收到消息监听
     */
    EMMessageListener msgListener = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {
//            //收到消息
//            // notify new message
//            for (EMMessage message : messages) {
//                //收到消息震动和播放音
//                HxHelper.getInstance().getNotifier().vibrateAndPlayTone(message);
//            }
            refreshUIWithMessage();
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> messages) {
            //收到透传消息
        }

        @Override
        public void onMessageRead(List<EMMessage> messages) {
            //收到已读回执
        }

        @Override
        public void onMessageDelivered(List<EMMessage> message) {
            //收到已送达回执
        }

        @Override
        public void onMessageRecalled(List<EMMessage> messages) {
            //消息被撤回
            refreshUIWithMessage();
        }

        @Override
        public void onMessageChanged(EMMessage message, Object change) {
            //消息状态变动

        }
    };


}